package com.yu.service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yu.bean.AdminExample.Criteria;
import com.yu.bean.User;
import com.yu.bean.UserExample;
import com.yu.dao.UserMapper;

/**
 * service 层
 * @author YUjiangpeng
 *
 */
@Service
public class UserService {
	
	@Autowired UserMapper userMapper;

	public User getUserByNameAndPwd(String userName, String pwd) {
		// TODO Auto-generated method stub
		//如果为手机号
		String regPhone = "^1\\d{10}$";
		//如果为邮箱
		String regMail= "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$";
		//如果为 用户昵称
		String regName ="^(?!_)(?!.*?_$)([a-zA-Z0-9_\\u4e00-\\u9fa5]{4,10}|[a-zA-Z0-9_]{1,2}[\\u4e00-\\u9fa5]{1,2}|[\\u4e00-\\u9fa5]{1,2}[a-zA-Z0-9_]{1,2}|[\\u4e00-\\u9fa5]{2,10}|[a-zA-Z0-9_]{4,20})$";
		if (userName.matches(regPhone)) {
			UserExample userExample3 = new UserExample();
			com.yu.bean.UserExample.Criteria criteria3 = userExample3.createCriteria();
			criteria3.andPhoneEqualTo(userName);
			criteria3.andPwdEqualTo(pwd);
			List<User> us3 =new ArrayList<>();
			us3 = userMapper.selectByExample(userExample3);
			if (us3.size()!=0) {
				return us3.get(0);
			}else{
				return null;
			}

		}else if(userName.matches(regName)) {
			UserExample userExample = new UserExample();
			com.yu.bean.UserExample.Criteria criteria =userExample.createCriteria();
			criteria.andUserNameEqualTo(userName);
			criteria.andPwdEqualTo(pwd);
			List<User> us1 =new ArrayList<>();
			us1 = userMapper.selectByExample(userExample);
			if (us1.size()!=0) {
				return us1.get(0);
			}else{
				return null;
			}
		}else if (userName.matches(regMail)) {
			List<User> us2 =new ArrayList<>();
			UserExample userExample2 = new UserExample();
			com.yu.bean.UserExample.Criteria criteria2 = userExample2.createCriteria();
			criteria2.andEmailEqualTo(userName);
			criteria2.andPwdEqualTo(pwd);
			 us2 =userMapper.selectByExample(userExample2);
			 if (us2.size()!=0) {
					return us2.get(0);
				}else{
					return null;
				}
		}else {
			return null;
		}
		
	}
	
	/**
	 * 检查邮箱是否已经存在
	 * @param email
	 * @return
	 */
	public Boolean checkMail(String email) {
		// TODO Auto-generated method stub
		UserExample example = new UserExample();
		com.yu.bean.UserExample.Criteria criteria = example.createCriteria();
		criteria.andEmailEqualTo(email);
		//查询符合条件的个数
		long counts =userMapper.countByExample(example);
		
		return counts == 0;
	}

	
	/**
	 * 检查用户昵称是否存在
	 * @param userName
	 * @return
	 */
	public boolean checkUname(String userName) {
		// TODO Auto-generated method stub
		UserExample example = new UserExample();
		com.yu.bean.UserExample.Criteria criteria = example.createCriteria();
		criteria.andUserNameEqualTo(userName);
		long counts =  userMapper.countByExample(example);
		return counts == 0;
	}
	
	
	
	/**
	 * 新增注册用户
	 * @param user
	 */
	public void saveUser(User user ) {
		// TODO Auto-generated method stub
		 user.setRegDate(new java.util.Date());
		 if(Math.random()*10<5)
			 user.setSex("F");
		 else
			 user.setSex("M");
		userMapper.insertSelective(user);
		
	}
	
	

	/**
	 * 登录时判断对应用户是否存在
	 * @param userName
	 * @return
	 */
	public Boolean checkUMP(String userName) {
		// TODO Auto-generated method stub
		//如果为手机号
				String regPhone = "^1\\d{10}$";
				//如果为邮箱
				String regMail= "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$";
				//如果为 用户昵称
				String regName ="^(?!_)(?!.*?_$)([a-zA-Z0-9_\\u4e00-\\u9fa5]{4,10}|[a-zA-Z0-9_]{1,2}[\\u4e00-\\u9fa5]{1,2}|[\\u4e00-\\u9fa5]{1,2}[a-zA-Z0-9_]{1,2}|[\\u4e00-\\u9fa5]{2,10}|[a-zA-Z0-9_]{4,20})$";
			 if (Pattern.matches(regPhone, userName)) {
					System.out.println("I'm here!!!");
					UserExample userExample3 = new UserExample();
					com.yu.bean.UserExample.Criteria criteria3 = userExample3.createCriteria();
					criteria3.andPhoneEqualTo(userName);
					List<User> us3 =new ArrayList<>();
					us3 = userMapper.selectByExample(userExample3);
					if (us3.size()!=0) {
						return true;
					}else{
						return false;
					}
				}else if (userName.matches(regName)) {
					UserExample userExample = new UserExample();
					com.yu.bean.UserExample.Criteria criteria =userExample.createCriteria();
					criteria.andUserNameEqualTo(userName);
					List<User> us1 =new ArrayList<>();
					us1 = userMapper.selectByExample(userExample);
					if (us1.size()!=0) {
						return true;
					}else{
						return false;
					}
				}else if (userName.matches(regMail)) {
					List<User> us2 =new ArrayList<>();
					UserExample userExample2 = new UserExample();
					com.yu.bean.UserExample.Criteria criteria2 = userExample2.createCriteria();
					criteria2.andEmailEqualTo(userName);
					 us2 =userMapper.selectByExample(userExample2);
					 if (us2.size()!=0) {
							return true;
						}else{
							return false;
						}
				}else {
					return false;
				}
	}

	/**
	 * 根据用户id 修改用户头像
	 * @param user
	 * @param storUrl
	 */
	public void uploadUpic(User user, String storUrl) {
		// TODO Auto-generated method stub
		user.setUpic(storUrl);
		userMapper.updateByPrimaryKeySelective(user);
		
	}



}
